
package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.service.IStationNeedsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.StationNeeds;
import com.xmy.cultivate.entity.response.TidyRs;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStationNeedsService;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.SchoolNeeds;
import com.xmy.cultivate.entity.TryJob;
import com.xmy.cultivate.service.ISchoolNeedsService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 校区招聘需求表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/api/schoolNeeds")
public class SchoolNeedsController extends BaseController {


    @Autowired
    ISchoolNeedsService iSchoolNeedsService;

    @Autowired
    IStationNeedsService iStationNeedsService;

    @Autowired
    IQuarterService iQuarterService;

    /**
     * 获取分配数据
     *
     * @return
     */
    @RequestMapping("/querySchoolNeedList")
    public Result querySchoolNeedList(@RequestParam(value = "startDate", required = false) String startDate,
                                      @RequestParam(value = "endDate", required = false) String endDate,
                                      @RequestParam(value = "stationId", required = false) String stationId,// 职位id
                                      @RequestParam(value = "type", required = false) Integer type,// 1为校区招聘岗位，2为部门招聘岗位
                                      @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                      @RequestParam(value = "labourId", required = false) String labourId, // 劳动关系
                                      @RequestParam(value = "schoolId", required = false) String schoolId, // 校区id
                                      @RequestParam(value = "departmentId", required = false) String departmentId, // 部门id
                                      @RequestParam(value = "stationNeedsId", required = false) String stationNeedsId,
                                      @RequestParam(value = "isDetails", required = false, defaultValue = "0") Integer isDetails // 是否为详情，0否1是
    ) {

        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            startDate = startDate + " 00:00:00";
            endDate = endDate + " 23:59:59";
        }

//        String stationNeedsId = iStationNeedsService.getIdsByLabourId(type, labourId, stationId, departmentId);
//
//        if (StringUtils.isBlank(stationNeedsId)) {
//            return new Result(Code.OK, new ArrayList<>(), Code.GET_OK_MSG);
//        }

        List<SchoolNeeds> schoolNeedsList = iSchoolNeedsService.getAssignList(startDate, endDate, stationNeedsId, schoolId, departmentId, isDetails, type, subjectsId, labourId, stationId);

        List<HashMap<String, Object>> list = new ArrayList<>();
        for (SchoolNeeds schoolNeeds : schoolNeedsList) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", schoolNeeds.getId());
            hashMap.put("schoolOrDepartmentId", schoolNeeds.getSchoolOrDepartmentId());
            hashMap.put("schoolOrDepartmentName", schoolNeeds.getSchoolOrDepartmentName());
            hashMap.put("stationType", schoolNeeds.getStationType());
            hashMap.put("subjectsId", schoolNeeds.getSubjectsId());
            hashMap.put("needCount", schoolNeeds.getNeedCount());
            hashMap.put("allocateCount", schoolNeeds.getAllocateCount());
            hashMap.put("tryJobPassCount", schoolNeeds.getTryJobPassCount());
            hashMap.put("tryJobNoPassCount", schoolNeeds.getTryJobNoPassCount());
            hashMap.put("needRecruitment", schoolNeeds.getNeedRecruitment());
            hashMap.put("departmentCount", schoolNeeds.getDepartmentCount());
            hashMap.put("schoolCount", schoolNeeds.getSchoolCount());
            hashMap.put("tryJobIngCount", schoolNeeds.getTryJobIngCount());
            hashMap.put("stationNeedsId", schoolNeeds.getStationId());
            hashMap.put("status", schoolNeeds.getStatus());
            hashMap.put("stationNeedsName", schoolNeeds.getStationNeedsName());
            hashMap.put("pid", schoolNeeds.getPid());

            list.add(hashMap);
        }
        return new Result(Code.OK, list, "获取数据成功");
    }

    /**
     * 分配数据详情
     *
     * @return
     */
    @RequestMapping("/querySchoolNeedDetails")
    public Result querySchoolNeedDetails(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "type", required = false) Integer type,// 1为校区招聘岗位，2为部门招聘岗位
            @RequestParam(value = "detailsType", required = false, defaultValue = "1") String detailsType,  //1部门分，2校区招，3试岗通关，4试岗淘汰，5试岗中
            @RequestParam(value = "subjectsId", required = false) String subjectsId,
            @RequestParam(value = "labourId", required = false) String labourId, // 劳动关系
            @RequestParam(value = "schoolId", required = false) String schoolId, // 校区id
            @RequestParam(value = "departmentId", required = false) String departmentId, // 部门id
            @RequestParam(value = "stationId", required = false) String stationId,
            @RequestParam(value = "stationNeedsId", required = false) String stationNeedsId
    ) {

        IPage<TryJob> page = new Page<>(pageNum, pageSize);

        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            startDate = startDate + " 00:00:00";
            endDate = endDate + " 23:59:59";
        }

        List<TryJob> tryJobList = iSchoolNeedsService.getAssignDetails(page, startDate, endDate, schoolId, departmentId, detailsType, type, labourId, stationId, stationNeedsId, subjectsId);

        List<HashMap<String, Object>> list = new ArrayList<>();
        for (TryJob tryJob : tryJobList) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", tryJob.getId());
            hashMap.put("tryDate", tryJob.getTryDate());
            hashMap.put("schoolId", tryJob.getSchoolId());
            hashMap.put("schoolName", tryJob.getSchoolName());
            hashMap.put("name", tryJob.getName());
            hashMap.put("phone", tryJob.getPhone());
            hashMap.put("subjectsId", tryJob.getSubjectsId());
            hashMap.put("stationId", tryJob.getStationId());
            hashMap.put("stationName", tryJob.getStationName());
            hashMap.put("tryAuditDate", tryJob.getTryAuditDate());
            hashMap.put("invitationName", tryJob.getInvitationName());
            list.add(hashMap);
        }
        return new Result(Code.OK, new PageResult<>(list, page.getTotal(), page.getPages()), Code.GET_OK_MSG);
    }

    @GetMapping("/getDate")
    @ApiOperation("获取试岗最大、最小日期")
    public Result getDate(@RequestParam Map<String, String> reMap) {
        TryJob tryJob = iSchoolNeedsService.getDate();

        HashMap hashMap = new HashMap<>();

        if (tryJob == null) {
            return new Result(Code.OK, hashMap, Code.GET_OK_MSG);
        }

        hashMap.put("minDate", tryJob.getMinDate());
        hashMap.put("maxDate", tryJob.getMaxDate());

        return new Result(Code.OK, hashMap, Code.GET_OK_MSG);
    }

    @PostMapping("/addOrUpdateData")
    public Result addOrUpdateData(@RequestBody SchoolNeeds schoolNeeds) {

        QueryWrapper<SchoolNeeds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolNeeds.getSchoolId());
        queryWrapper.eq("station_needs_id", schoolNeeds.getStationNeedsId());
        queryWrapper.last("limit 1");
        SchoolNeeds schoolNeedsOld = iSchoolNeedsService.getOne(queryWrapper);
        if (Objects.isNull(schoolNeedsOld)) {
            StationNeeds stationNeeds = iStationNeedsService.getById(schoolNeeds.getStationNeedsId());
            if (Objects.isNull(stationNeeds)) {
                return new Result(Code.ERROR, "", "职位不存在");
            }

            schoolNeedsOld = new SchoolNeeds();
            schoolNeedsOld.setYearPart(schoolNeeds.getYearPart());
            schoolNeedsOld.setQuarterNum(schoolNeeds.getQuarterNum());
            schoolNeedsOld.setSchoolId(schoolNeeds.getSchoolId());
            schoolNeedsOld.setNeedCount(schoolNeeds.getNeedCount());
            schoolNeedsOld.setSubjectsId(stationNeeds.getSubjectsId());

            schoolNeedsOld.setDepartmentId(stationNeeds.getDepartmentId());
            schoolNeedsOld.setStationId(stationNeeds.getStationId());
            schoolNeedsOld.setStationNeedsId(stationNeeds.getId());
            iSchoolNeedsService.save(schoolNeedsOld);

        } else {
            schoolNeedsOld.setNeedCount(schoolNeeds.getNeedCount());
            iSchoolNeedsService.updateById(schoolNeedsOld);
        }
        return new Result(Code.OK, "", Code.GET_OK_MSG);
    }

    @GetMapping("/getAllNeedsSchool")
    public Result getAllNeedsSchool() {
        Quarter quarterNow = iQuarterService.getNowQuarter();
        List<TidyRs> tidyRsList = iSchoolNeedsService.getAllNeedsSchool(quarterNow.getYearPart(), quarterNow.getNum());
        return new Result(Code.OK, tidyRsList, Code.GET_OK_MSG);
    }

}
